using IM.Easy.Infrastructure.Query;
using Microsoft.AspNetCore.Mvc;
using IM.Easy.SystemServices.IServices;
using IM.Easy.Entity.System;
using IM.Easy.SystemServices.Dtos;
using IM.Easy.Core.Attributes;

namespace IM.Easy.Api.Controllers.system
{
    /// <summary>
    /// SysRoleMenuController 控制器
    /// </summary>
    [Route("system/permission/menu")]
    public class RoleMenuController : ControllerBase
    {
        private readonly IRoleMenuService _service;

        /// <summary>
        /// 构造函数，注入服务
        /// </summary>
        /// <param name="service">服务接口</param>
        public RoleMenuController(IRoleMenuService service)
        {
            _service = service;
        }

        /// <summary>
        /// 获取所有SysRoleMenu对象
        /// </summary>
        /// <returns>返回SysRoleMenu列表</returns>
        [HttpGet]
        [Obsolete]
        public PageResponse<SysRoleMenu> GetList([FromQuery] RoleMenuQueryDto query)
        {
            return _service.GetPageList(query);
        }

        /// <summary>
        /// 根据ID获取SysRoleMenu对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <returns>返回指定的SysRoleMenu对象</returns>
        [HttpGet("{id}")]
        [Obsolete]
        public SysRoleMenu GetById([FromRoute] long id)
        {
            var result = _service.GetById(id);
            return result ?? throw new KeyNotFoundException($"未找到ID为 {id} 的SysRoleMenu对象。");
        }

        /// <summary>
        /// 创建新的SysRoleMenu对象
        /// </summary>
        /// <param name="entity">要创建的SysRoleMenu对象</param>
        /// <returns>返回创建后的SysRoleMenu对象</returns>
        [HttpPost]
        [Obsolete]
        public SysRoleMenu Create([FromBody] SysRoleMenu entity)
        {
            _service.Create(entity);
            return entity; // 直接返回创建的实体
        }

        /// <summary>
        /// 更新现有的SysRoleMenu对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        /// <param name="entity">更新后的SysRoleMenu对象</param>
        [HttpPut]
        [Obsolete]
        public void Update([FromBody] SysRoleMenu entity)
        {
            _service.Update(entity);
        }

        /// <summary>
        /// 批量删除SysRoleMenu对象
        /// </summary>
        /// <param name="ids">对象的IDS</param>
        [HttpDelete]
        [Obsolete]
        public void Delete([FromBody] long[] ids)
        {
            _service.Delete(ids);
        }

        /// <summary>
        /// 单个删除SysRoleMenu对象
        /// </summary>
        /// <param name="id">对象的ID</param>
        [HttpDelete("{id}")]
        [Obsolete]
        public void Delete([FromRoute] long id)
        {
            _service.Delete(id);
        }

        /// <summary>
        /// 保存
        /// </summary>
        [HttpPost("save/{roleId}")]
        [Api(Name = "保存角色菜单", Perm = "role:menu:save")]
        public void Save([FromRoute] long roleId, [FromBody] params long[] menuIds)
        {
            _service.Save(roleId, menuIds);
        }

        /// <summary>
        /// 获取角色已分配菜单
        /// </summary>
        /// <param name="roleId"></param>
        [HttpGet("assigned/{roleId}")]
        [Api(Name = "获取角色已分配菜单", Perm = "role:menu:assigned")]
        public List<SysMenu> GetAssignedMenus([FromRoute] long roleId)
        {
            return _service.GetMenus(roleId);
        }
    }
}
